package com.shigen.demo.atguigu;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * 单个元素的队列，不消费不生产
 *
 * @author shigenfu
 * @date 2022/5/22 9:13 下午
 */
public class SynchronousQueueDemo {

  public static void main(String[] args) {
    SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

    new Thread(() -> {
      try {
        System.out.println(Thread.currentThread().getName() + "put aa");
        synchronousQueue.put("aa");
        System.out.println(Thread.currentThread().getName() + "put bb");
        synchronousQueue.put("bb");
        System.out.println(Thread.currentThread().getName() + "put cc");
        synchronousQueue.put("cc");
      }catch (Exception e) {
        e.printStackTrace();
      }
    }, "aaa").start();

    new Thread(() -> {
      try {
        TimeUnit.SECONDS.sleep(5);
        System.out.println(synchronousQueue.take());
        TimeUnit.SECONDS.sleep(5);
        System.out.println(synchronousQueue.take());
        TimeUnit.SECONDS.sleep(5);
        System.out.println(synchronousQueue.take());
      }catch (Exception e) {
        e.printStackTrace();
      }
    }, "bbb").start();



  }

}
